package com.dataseek.iot.sys.manager.handler;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.core.handler.ServiceHandler;
import com.dataseek.iot.mysql.entity.Area;
import com.dataseek.iot.mysql.entity.Line;
import com.dataseek.iot.mysql.entity.User;
import com.dataseek.iot.mysql.service.impl.DeviceServiceImpl;
import com.dataseek.iot.mysql.service.impl.LineServiceImpl;
import com.dataseek.iot.mysql.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Objects;

/**
 * @author cc
 * @Title: 线路服务 handler
 * @Description: 线路服务 handler
 * @date 2021/9/69:19 下午
 * @since 1.0
 */
@Component
@Slf4j
public class LineServiceHandler extends ServiceHandler<Line> {

    @Autowired
    LineServiceImpl lineService;

    @Autowired
    UserServiceImpl userService;

    @Autowired
    DeviceServiceImpl deviceService;

    @Override
    public Mono<ResponseEntity> saveOne(Object entity) {
        ResponseEntity responseEntity = new ResponseEntity();
        Line l = (Line) entity;
        QueryWrapper  wrapper = new QueryWrapper();
        wrapper.eq("line_code", l.getLineCode());
        wrapper.or(true).eq("line_name", l.getLineName());
        if(this.isExist(wrapper)){
            responseEntity.setCode(ResponseConst.DUPLICATION_CODE);
            responseEntity.setMsg(ResponseConst.DUPLICATION);
            responseEntity.setData("");
            return Mono.just(responseEntity);
        };
        Boolean b = lineService.save((Line) entity);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> saveBatch(List lists) {
        ResponseEntity responseEntity = new ResponseEntity();
        for(Object line : lists){
            Line l = (Line)line;
            QueryWrapper  wrapper = new QueryWrapper();
            wrapper.eq("line_code", l.getLineCode());
            wrapper.or(true).eq("line_name", l.getLineName());
            if(this.isExist(wrapper)){
                responseEntity.setCode(ResponseConst.DUPLICATION_CODE);
                responseEntity.setMsg(ResponseConst.DUPLICATION);
                responseEntity.setData("");
                return Mono.just(responseEntity);
            }
        }
        Boolean b = lineService.saveBatch(lists);
        return this.createResponse(b);
    }

    /**
     * 根据线路编码删除
     * @param code
     * @return
     */
    public Mono<ResponseEntity> removeByCode(String code){
        ResponseEntity entity = new ResponseEntity();
        QueryWrapper queryWrapper  = new QueryWrapper();
        queryWrapper.eq("line_code", code);
        List list = userService.list(queryWrapper);
        if(list.size()>0) {
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg("待删除线路信息中有用户关联，请先修改相关用户");
            entity.setData(list);
            return Mono.just(entity);
        }else {
            List devices = deviceService.list(queryWrapper);
            if(devices.size()>0){
                entity.setCode(ResponseConst.CRUD_FAILED_CODE);
                entity.setMsg("待删除线路信息中有设备关联，请先修改相关设备");
                entity.setData(devices);
                return Mono.just(entity);
            }
            queryWrapper = new QueryWrapper();
            queryWrapper.eq("line_code", code);;
            Boolean b = lineService.remove(queryWrapper);
            return this.createResponse(b);
        }
    }

    @Override
    public Mono<ResponseEntity> removeById(String id) {
        Line line = lineService.getById(id);
        ResponseEntity entity = new ResponseEntity();
        if(Objects.isNull(line)){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg("未查询到线路");
            entity.setData("");
            return Mono.just(entity);
        }
        return removeByCode(line.getLineCode());
    }

    /**
     * 根据线路编码批量删除
     * @param codes
     * @return
     */
    public Mono<ResponseEntity> removeAllByCodes(List codes){
        ResponseEntity entity = new ResponseEntity();
        QueryWrapper queryWrapper  = new QueryWrapper();
        queryWrapper.in("line_code", codes);
        List list = userService.list(queryWrapper);
        if(list.size()>0){
            entity.setCode(ResponseConst.CRUD_FAILED_CODE);
            entity.setMsg("待删除线路中有用户关联，请先修改相关用户信息");
            entity.setData(list);
            return Mono.just(entity);
        }else{
            List devices = deviceService.list(queryWrapper);
            if(devices.size()>0) {
                entity.setCode(ResponseConst.CRUD_FAILED_CODE);
                entity.setMsg("待删除线路信息中有设备关联，请先修改相关设备");
                entity.setData(devices);
                return Mono.just(entity);
            }
            Boolean b = lineService.remove(queryWrapper);
            return this.createResponse(b);
        }
    }

    @Override
    public Mono<ResponseEntity> removeAll(List ids) {
        for(Object id : ids){
            lineService.removeById(id.toString());
        }
        Boolean b = lineService.removeByIds(ids);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> updateOne(Object entity) {
        Boolean b = lineService.updateById((Line) entity);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> updateBatch(List entities) {
        Boolean b =  lineService.updateBatchById(entities);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> findOne(String id) {
        Line line = lineService.getById(id);
        return this.createResponse(line);
    }

    @Override
    public Mono<ResponseEntity> page(Page p) {
        Page authPage = lineService.page(p);
        return this.createResponse(authPage);
    }

    @Override
    public Mono<ResponseEntity> page(Page p, Wrapper wrapper) {
        Page linePage = lineService.page(p, wrapper);
        return this.createResponse(linePage);
    }

    @Override
    public Flux<ResponseEntity> find(List ids) {
        List<Line> lists = lineService.listByIds(ids);
        return this.createResponse(lists);
    }

    @Override
    public Flux<ResponseEntity> find(Wrapper wrapper) {
        List<Line> lists = lineService.list(wrapper);
        return this.createResponse(lists);
    }

    @Override
    public Flux findAll() {
        List<Line> lists = lineService.list();
        return this.createResponse(lists);
    }

    @Override
    public Mono<ResponseEntity> remove(Wrapper wrapper) {
        List<Line> lines = lineService.list(wrapper);
        ResponseEntity entity = new ResponseEntity();
        for (Line l : lines) {
            List<User> users = userService.getUsersByLineCode(l.getLineCode());
            if (users.size() > 0) {
                log.error("待删除线路信息中有用户关联，请先修改相关用户");
                entity.setMsg("待删除线路信息中有用户关联，请先修改相关用户");
                entity.setCode(ResponseConst.CRUD_FAILED_CODE);
                entity.setData(lines);
                return Mono.just(entity);
            } else {
                List devices = deviceService.getDeviceBylineCode(l.getLineCode());
                if (devices.size() > 0) {
                    entity.setCode(ResponseConst.CRUD_FAILED_CODE);
                    entity.setMsg("待删除线路信息中有设备关联，请先修改相关设备");
                    entity.setData(devices);
                    return Mono.just(entity);
                }
                Boolean b = lineService.remove(wrapper);
            }
        }
        return this.createResponse(true);
    }

    @Override
    public Mono<ResponseEntity> remove(String id) {
        Boolean b = lineService.removeById(id);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> update(Wrapper wrapper) {
        Boolean b = lineService.update(wrapper);
        return this.createResponse(b);
    }

    @Override
    public Mono<ResponseEntity> defaultResponseOne(ResponseEntity responseEntity) {
        return Mono.just(responseEntity);
    }

    @Override
    public Flux<ResponseEntity> defaultResponseTwo(ResponseEntity responseEntity) {
        return Flux.just(responseEntity);
    }

    /**
     * 验证是否存在
     * @param wrapper
     * @return
     */
    public Boolean isExist(QueryWrapper wrapper){
        if(Objects.nonNull(lineService.getOne(wrapper))) return true;
        return false;
    }
}
